package com.zoro.barn.business.service.pp.business.product.controller;

import com.zoro.barn.business.service.pp.business.product.controller.dto.AddUserDto;
import com.zoro.barn.business.service.pp.business.product.controller.dto.GerUsersRes;
import com.zoro.barn.business.service.pp.business.product.entity.BarnProductUser;
import com.zoro.barn.business.service.pp.server.UserServerService;
import com.zoro.barn.business.service.pp.business.product.service.BarnProductUserService;
import com.zoro.barn.dto.system.BarnUserInfoDto;
import com.zoro.barn.dto.system.userinfodto.GetUserInfosReq;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author zhaoxingwu
 */
@RestController
@RequestMapping("/product-user")
public class BarnProductUserController {

    private final BarnProductUserService barnProductUserService;
    private final UserServerService userServerService;

    public BarnProductUserController(BarnProductUserService barnProductUserService, UserServerService userServerService) {
        this.barnProductUserService = barnProductUserService;
        this.userServerService = userServerService;
    }

    @RequestMapping("/addUsers")
    public void addUsers(@RequestBody AddUserDto addUserDto) {
        this.barnProductUserService.addUsers(addUserDto.getProductId(), addUserDto.getUserIds());
    }

    @RequestMapping("/getUsers/{productId}")
    public List<GerUsersRes> getUsers(@PathVariable String productId) throws Exception {

        List<BarnProductUser> users = this.barnProductUserService.getUsers(productId);
        if (CollectionUtils.isEmpty(users)) {
            return null;
        }
        Set<String> userIds = users.stream().map(BarnProductUser::getUserId).collect(Collectors.toSet());
        GetUserInfosReq req = new GetUserInfosReq();
        req.setIds(userIds.toArray(new String[]{}));
        List<BarnUserInfoDto> userInfoDtos = userServerService.getUserInfos(req);
        if (CollectionUtils.isEmpty(userInfoDtos)) {
            return null;
        }
        return userInfoDtos.stream().map(u -> {
            GerUsersRes r = new GerUsersRes();
            r.setUserId(u.getPid());
            r.setUserName(u.getUserName());
            r.setUserAccount(u.getUserAccount());
            return r;
        }).collect(Collectors.toList());
    }

    @RequestMapping("/deleteUser")
    public void deleteUser(@RequestBody BarnProductUser productUser) {
        if (StringUtils.isAnyBlank(productUser.getProductId(), productUser.getUserId())) {
            return;
        }

        this.barnProductUserService.deleteUser(productUser.getProductId(), productUser.getUserId());

    }
}
